<!--
 * @Author: 深海有鱼 847094299@qq.com
 * @Date: 2025-04-18 18:28:20
 * @LastEditors: 深海有鱼 847094299@qq.com
 * @LastEditTime: 2025-07-14 09:02:44
 * @FilePath: \pure-admin-thin\src\views\adminService\components\ChangeSortDialog.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-dialog
    v-model="dialogVisible"
    title="自定义小时下载"
    width="400px"
    @close="resetForm"
  >
    <el-form ref="serviceForm" :model="form" :rules="rules">
      <el-form-item label="小时" prop="recentHours">
        <el-input
          v-model="form.recentHours"
          type="number"
          placeholder="请输入小时"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false">取消</el-button>
      <el-button type="primary" @click="handleConfirm">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref } from "vue";
import { downloadHoursResultFile } from "@/api/taskApi";
import { message } from "@/utils/message";

const dialogVisible = ref(false);
const taskId = ref(0);
const form = ref({
  recentHours: 24
});

// 基础校验规则
const rules = {
  recentHours: [
    { required: true, message: "请输入小时", trigger: "blur" },
    { pattern: /^[1-9]\d*$/, message: "请输入正确的数字", trigger: "blur" },
    {
      validator: (rule, value, callback) => {
        if (value < 1 || value > 24) {
          callback(new Error("请输入1-24之间的整数"));
        } else {
          callback();
        }
      },
      trigger: "blur"
    }
  ]
};

const serviceForm = ref(null);
// 重置表单
const resetForm = () => {
  serviceForm.value?.resetFields();
};

const handleConfirm = () => {
  serviceForm.value.validate(async valid => {
    if (valid) {
      downloadHoursResultFile(taskId.value, form.value);
      message("下载中，请稍后...", { type: "success" });
      dialogVisible.value = false;
    }
  });
};
const open = id => {
  taskId.value = id;
  dialogVisible.value = true;
};
defineExpose({
  open
});
</script>
